package com.jx.service.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jx.dao.system.MenuDao;
import com.jx.pojo.entity.system.Menu;
import com.jx.pojo.enums.system.MenuTypeEnum;
import com.jx.pojo.po.system.MenuPO;
import com.jx.pojo.po.system.MenuTree;
import com.jx.pojo.vo.system.MenuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 菜单 服务
 */
@Service
@Slf4j
public class MenuService extends ServiceImpl<MenuDao, Menu> {


    /**
     * 获取菜单列表
     */
    public List<MenuPO> listMenu(MenuVO query) {
        // 查询出菜单列表
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(query.getMenuName()), "menu_name", query.getMenuName());
        wrapper.eq(query.getStatus() != null, "status", query.getStatus());
        List<Menu> list = this.list(wrapper);

        // 按照级别进行分组
        List<MenuPO> result = new ArrayList<>();
        // 目录一级
        for (Menu menu : list) {
            Integer menuType = menu.getMenuType();
            if (MenuTypeEnum.DIRECTORY.getValue().equals(menuType)) {
                MenuPO po = new MenuPO();
                BeanUtils.copyProperties(menu, po);
                result.add(po);
            }
        }
        if (!CollectionUtils.isEmpty(result)) {
            result = result.stream().sorted(Comparator.comparingInt(MenuPO::getSort)).collect(Collectors.toList());
        }

        // 菜单一级
        Map<String, List<MenuPO>> menuPOMap = new HashMap<>();
        for (Menu menu : list) {
            Integer menuType = menu.getMenuType();
            if (MenuTypeEnum.MENU.getValue().equals(menuType)) {
                MenuPO po = new MenuPO();
                BeanUtils.copyProperties(menu, po);
                Integer parentId = menu.getParentId();
                List<MenuPO> menuPOList = menuPOMap.get(parentId + "");
                if (CollectionUtils.isEmpty(menuPOList)) {
                    menuPOList = new ArrayList<>();
                }
                menuPOList.add(po);
                menuPOMap.put(parentId + "", menuPOList);
            }
        }

        for (MenuPO po : result) {
            List<MenuPO> menuPOList = menuPOMap.get(po.getId() + "");
            if (CollectionUtils.isEmpty(menuPOList)) {
                continue;
            }
            po.setChildren(menuPOList.stream().sorted(Comparator.comparingInt(MenuPO::getSort)).collect(Collectors.toList()));
        }

        return result;
    }

    public void saveMenu(Menu menu) {
        Integer id = menu.getId();
        LocalDateTime dateTime = LocalDateTime.now();
        if (id == null) {
            menu.setCreateTime(dateTime);
        }
        menu.setUpdateTime(dateTime);
        this.saveOrUpdate(menu);
    }

    /**
     * 获取目录列表
     */
    public List<Object> getDirList() {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_type", MenuTypeEnum.DIRECTORY);
        wrapper.orderByAsc("sort");
        List<Menu> list = this.list(wrapper);
        List<Object> result = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("value", 0);
        map.put("label", "主目录");
        result.add(map);
        for (Menu menu : list) {
            Map<String, Object> item = new HashMap<>();
            item.put("value", menu.getId());
            item.put("label", menu.getMenuName());
            result.add(item);
        }
        return result;
    }

    /**
     * 获取菜单树
     */
    public List<MenuTree> getMenuTree() {
        // 查询出菜单列表
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        List<Menu> list = this.list(wrapper);

        // 按照级别进行分组
        List<MenuTree> result = new ArrayList<>();
        // 目录一级
        for (Menu menu : list) {
            Integer menuType = menu.getMenuType();
            if (MenuTypeEnum.DIRECTORY.getValue().equals(menuType)) {
                MenuTree po = new MenuTree();
                po.setId(menu.getId());
                po.setLabel(menu.getMenuName());
                po.setSort(menu.getSort());
                result.add(po);
            }
        }
        if (!CollectionUtils.isEmpty(result)) {
            result = result.stream().sorted(Comparator.comparingInt(MenuTree::getSort)).collect(Collectors.toList());
        }

        // 菜单一级
        Map<String, List<MenuTree>> menuPOMap = new HashMap<>();
        for (Menu menu : list) {
            Integer menuType = menu.getMenuType();
            if (MenuTypeEnum.MENU.getValue().equals(menuType)) {
                MenuTree po = new MenuTree();
                po.setId(menu.getId());
                po.setLabel(menu.getMenuName());
                po.setSort(menu.getSort());
                Integer parentId = menu.getParentId();
                List<MenuTree> menuPOList = menuPOMap.get(parentId + "");
                if (CollectionUtils.isEmpty(menuPOList)) {
                    menuPOList = new ArrayList<>();
                }
                menuPOList.add(po);
                menuPOMap.put(parentId + "", menuPOList);
            }
        }

        for (MenuTree po : result) {
            List<MenuTree> menuPOList = menuPOMap.get(po.getId() + "");
            if (CollectionUtils.isEmpty(menuPOList)) {
                continue;
            }
            po.setChildren(menuPOList.stream().sorted(Comparator.comparingInt(MenuTree::getSort)).collect(Collectors.toList()));
        }
        return result;
    }
}
